from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
db = SQLAlchemy()

# 基类
class BaseModel(object):
    """模型基类，为每个模型补充创建时间与更新时间"""
    create_time = db.Column(db.DateTime, default=datetime.now)  # 记录的创建时间
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)  # 记录的更新时间

# 法院表
class Court(db.Model):
    __tablename__ = "tb_courts"
    # 法院id
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    # 法院编号
    number = db.Column(db.String(40),unique=True,nullable=False)
    # 法院名称
    name = db.Column(db.String(40),unique=True,nullable=False)
    # 在职人员数量
    inservicestaffnum = db.Column(db.Integer,default=0)
    # 请假人员数量
    leavestaffnum = db.Column(db.Integer,default=0)
    # 地区
    area = db.Column(db.String(20))
    # 父级法院
    parent_id = db.Column(db.Integer, db.ForeignKey("tb_courts.id"),nullable=True)
    # 父法院
    parent = db.relationship('Court', remote_side=[id], backref='childs')
    # 法院级别，便于检索
    score = db.Column(db.SmallInteger)

# 部门模型
class Depart(db.Model):
    __tablename__ = "tb_depart"
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    name = db.Column(db.String(50))
    court_id = db.Column(db.Integer, db.ForeignKey("tb_courts.id"), nullable=False)
    court = db.relationship("Court", backref='departs')


# 管理员模型
class AdminUser(db.Model,BaseModel):
    __tablename__ = "tb_adminusers"
    # 管理员id
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    # 管理员用户名
    name = db.Column(db.String(30),unique=True,nullable=False)
    # 管理员密码
    password = db.Column(db.String(255),nullable=False)
    # 管理员分支，主管理员为1，从管理员为2
    tag = db.Column(db.SmallInteger,default=1)
    # 负责法院id
    court_id = db.Column(db.Integer,db.ForeignKey("tb_courts.id"),nullable=False)
    # 负责法院
    count = db.relationship('Court', backref='admins')

# 人员模型
class People(db.Model,BaseModel):
    __tablename__ = "tb_people"
    # 人员id
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 职工编号
    number = db.Column(db.String(90),nullable=False,unique=True)
    # 人员姓名
    name = db.Column(db.String(30))
    # 人员身份证号
    idcard = db.Column(db.String(29))
    # 身份证有效期
    idcard_valid_date = db.Column(db.Date)
    # 人员头像
    imgsrc = db.Column(db.String(255))
    # 人员电话
    phone = db.Column(db.String(60))
    # 出生日期
    both = db.Column(db.DATE)
    # 政治面貌
    politicsstatus = db.Column(db.String(50))
    # 婚姻状况
    marital = db.Column(db.SmallInteger)
    # 部门id
    depart_id = db.Column(db.Integer,db.ForeignKey('tb_courts.id'),nullable=False)
    # 部门
    depart = db.relationship('Depart',backref='peoples')
    # 备注
    remark = db.Column(db.String(255))

# # 人员图片模型
# class PeoplePic(db.Model,BaseModel):
#     __tablename__ = "tb_peoplepics"
#     # 人员图片id
#     id = db.Column(db.Integer,primary_key=True,autoincrement=True)
#     # 人员图片路径
#     imgsrc = db.Column(db.String(255))
#     # 是否为主图片，便于查询
#     is_main = db.Column(db.Boolean)
#     # 绑定人员id
#     people_id = db.Column(db.Integer,db.ForeignKey('tb_people.id'),nullable=False)
#     # 主要负责反键
#     people = db.relationship('People',backref='pics')




# 人员辅助信息模型
class Subsidiary(db.Model,BaseModel):
    __tablename__ = "tb_subsidiaries"
    # 人员辅助信息id
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 手机号
    phone = db.Column(db.String(20))
    # 邮箱号
    email = db.Column(db.String(20))
    # 人员id
    people_id = db.Column(db.Integer,db.ForeignKey('tb_people.id'),nullable=False)
    # 详细地址
    address = db.Column(db.String(255))
    # 人员
    people = db.relationship("People",backref='subsidiaries')
    # 备注
    remark = db.Column(db.String(255))

# 业绩模型
class Performance(db.Model,BaseModel):
    __tablename__ = "tb_performances"
    # 业绩id
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 业绩内容
    content = db.Column(db.Text)
    # 业绩时间
    perfor_time = db.Column(db.Integer)
    # 人员id
    people_id = db.Column(db.Integer, db.ForeignKey('tb_people.id'), nullable=False)
    # 人员
    people = db.relationship("People", backref='performances')

# 特长模型
class Speciality(db.Model,BaseModel):
    __tablename__ = "tb_specialities"
    # 特长id
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 特长名称
    name = db.Column(db.String(100))
    # 人员id
    people_id = db.Column(db.Integer, db.ForeignKey('tb_people.id'), nullable=False)
    # 人员
    people = db.relationship("People", backref='specialities')

# 工作经历模型
class WorkExperience(db.Model,BaseModel):
    __tablename__ = "tb_work_experiences"
    # 工作经历id
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 开始时间
    start_time = db.Column(db.DATE)
    # 结束时间
    finish_time = db.Column(db.DATE)
    # 工作单位
    company = db.Column(db.String(100))
    # 工作部门
    department = db.Column(db.String(100))
    # 任职情况
    work_peformance_information = db.Column(db.String(255))
    # 备注
    remark = db.Column(db.String(255))
    # 人员id
    people_id = db.Column(db.Integer, db.ForeignKey('tb_people.id'), nullable=False)
    # 人员
    people = db.relationship("People", backref='work_experiences')

# 教育经历管理模型
class EducationExperience(db.Model,BaseModel):
    __tablename__ = "tb_education_experiences"
    # 教育经历id
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 开始时间
    start_time = db.Column(db.DATE)
    # 结束时间
    finish_time = db.Column(db.DATE)
    # 学校名称
    school = db.Column(db.String(100))
    # 专业
    major = db.Column(db.String(100))
    # 学校情况
    work_peformance_information = db.Column(db.String(255),nullable=True)
    # 备注
    remark = db.Column(db.String(255),nullable=True)
    # 人员id
    people_id = db.Column(db.Integer, db.ForeignKey('tb_people.id'), nullable=False)
    # 人员
    people = db.relationship("People", backref='education_experiences')

# 社会关系信息管理
class SocialRelation(db.Model,BaseModel):
    __tablename__ = "tb_social_relations"
    # 社会关系id
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 关系人姓名
    name = db.Column(db.String(30))
    # 关系人关系
    relation = db.Column(db.String(30))
    # 工作单位
    company = db.Column(db.String(100))
    # 所任职务
    job = db.Column(db.String(100))
    # 政治面貌
    politics = db.Column(db.String(50))
    # 联系电话
    phone = db.Column(db.String(30))
    # 社会征信
    social_credit = db.Column(db.String(100))
    # 有无犯罪状况
    iscrime = db.Column(db.Boolean,default=False)
    # 备注
    remark = db.Column(db.String(255))
    # 人员id
    people_id = db.Column(db.Integer, db.ForeignKey('tb_people.id'), nullable=False)
    # 人员
    people = db.relationship("People", backref='social_relations')

# 劳动合同信息管理
class LaborContract(db.Model,BaseModel):
    __tablename__ = "tb_lobor_contracts"
    # 合同id
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    # 合同编号
    number = db.Column(db.String(255))
    # 合同名称
    name = db.Column(db.String(255))
    # 合同类型
    tag = db.Column(db.Boolean)
    # 签订时间
    sign_time = db.Column(db.DATE)
    # 合同状况
    sign_status = db.Column(db.Boolean)
    # 试用期失效日期
    probation_failure_time = db.Column(db.DATE)
    # 生效时间
    effect_time = db.Column(db.DATE)
    # 失效时间
    failure_time = db.Column(db.DATE)
    # 支付时间
    pay_time = db.Column(db.Date)
    # 支付方式
    pay_tag = db.Column(db.Boolean)
    # 工资等级
    salary_score = db.Column(db.Integer)
    # 是否缴纳社保
    if_pay_security = db.Column(db.Boolean)
    # 社保缴纳比例
    pay_security_part = db.Column(db.String(10))
    # 五险一金社会保障落实情况
    social_security_implementation = db.Column(db.String(255))
    # 法院部门id
    depart_id = db.Column(db.Integer, db.ForeignKey('tb_courts.id'), nullable=False)
    # 部门
    depart = db.relationship('Depart', backref='laborcontracts')
    # 备注
    remark = db.Column(db.String(255))
    # 人员id
    people_id = db.Column(db.Integer, db.ForeignKey('tb_people.id'), nullable=False)
    # 人员
    people = db.relationship("People", backref='lobor_contracts')


# 调动记录信息管理模型
class Transfer(db.Model,BaseModel):
    __tablename__ = "tb_transfers"
    # 调动记录id
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 调动开始日期
    start_time = db.Column(db.DATE)
    # 调动结束日期
    finish_time = db.Column(db.DATE)
    # 调动前单位
    company_before = db.Column(db.String(100))
    # 调动后单位
    company_after = db.Column(db.String(100))
    # 调动缘由
    reason = db.Column(db.Text)
    # 备注
    remark = db.Column(db.String(255))
    # 人员id
    people_id = db.Column(db.Integer, db.ForeignKey('tb_people.id'), nullable=False)
    # 人员
    people = db.relationship("People", backref='transfers')

# 考核信息管理模型
class Assessment(db.Model,BaseModel):
    __tablename__ = "tb_assessments"
    # 考核信息id
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 考核类型
    tag = db.Column(db.Integer)
    # 考核开始日期
    start_time = db.Column(db.Date)
    # 考核结束日期
    finish_time = db.Column(db.Date)
    # 考核结果
    result = db.Column(db.String(50))
    # 备注
    remark = db.Column(db.String(255))
    # 人员id
    people_id = db.Column(db.Integer, db.ForeignKey('tb_people.id'), nullable=False)
    # 人员
    people = db.relationship("People", backref='assessments')

# 培训记录信息管理模型
class TrainingRecord(db.Model,BaseModel):
    __tablename__ = "tb_training_records"
    # 培训记录id
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 培训机构
    tag = db.Column(db.String(100))
    # 培训开始日期
    start_time = db.Column(db.Date)
    # 培训结束日期
    finish_time = db.Column(db.Date)
    # 培训内容
    content = db.Column(db.Text)
    # 培训情况
    situation = db.Column(db.Text)
    # 备注
    remark = db.Column(db.String(255))
    # 人员id
    people_id = db.Column(db.Integer, db.ForeignKey('tb_people.id'), nullable=False)
    # 人员
    people = db.relationship("People", backref='training_records')

# 奖惩记录信息管理模型
class DisciplinaryRecord(db.Model,BaseModel):
    __tablename__ = "tb_disciplinary_records"
    # 奖惩记录id
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 奖惩日期
    disciplinary_time = db.Column(db.DATE)
    # 奖惩类型
    tag = db.Column(db.String(50))
    # 奖惩内容
    content = db.Column(db.Text)
    # 撤销日期
    repeal_time = db.Column(db.DATE)
    # 撤销缘由
    reason = db.Column(db.Text)
    # 备注
    remark = db.Column(db.String(255))
    # 人员id
    people_id = db.Column(db.Integer, db.ForeignKey('tb_people.id'), nullable=False)
    # 人员
    people = db.relationship("People", backref='disciplinary_records')

# 请假记录信息管理模型
class LeaveInfomation(db.Model,BaseModel):
    __tablename__ = "tb_leave_infomations"
    # 请假记录id
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 请假事由
    reason = db.Column(db.Text)
    # 请假申请时间
    register_time = db.Column(db.Date)
    # 请假开始日期
    start_time = db.Column(db.Date)
    # 请假天数
    length = db.Column(db.Integer)
    # 审批时间
    approval_time = db.Column(db.Date)
    # 审批部门
    depart = db.Column(db.String(60))
    # 备注
    remark = db.Column(db.String(255))
    # 人员id
    people_id = db.Column(db.Integer, db.ForeignKey('tb_people.id'), nullable=False)
    # 人员
    people = db.relationship("People", backref='leave_infomations')

# 装备信息管理模型
class Equipment(db.Model,BaseModel):
    __tablename__ = "tb_equipments"
    # 装备信息id
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 装备编号
    number = db.Column(db.String(100))
    # 装备名称
    name = db.Column(db.String(60))
    # 配备缘由
    reason = db.Column(db.Text)
    # 领取时间
    receive_time = db.Column(db.Date)
    # 注销时间
    out_time = db.Column(db.Date)
    # 备注
    remark = db.Column(db.String(255))
    # 人员id
    people_id = db.Column(db.Integer, db.ForeignKey('tb_people.id'), nullable=False)
    # 人员
    people = db.relationship("People", backref='equipments')

# 体检记录管理模型
class PhysicalExamination(db.Model,BaseModel):
    __tablename__ = "tb_physical_examinations"
    # 体检记录信息id
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 体检医院
    hospital = db.Column(db.String(100))
    # 体检日期
    physical_time = db.Column(db.Date)
    # 体检结果
    result = db.Column(db.Text)
    # 备注
    remark = db.Column(db.String(255))
    # 人员id
    people_id = db.Column(db.Integer, db.ForeignKey('tb_people.id'), nullable=False)
    # 人员
    people = db.relationship("People", backref='physical_examinations')

# 登录日志
class Loginlog(db.Model):
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    sid = db.Column(db.String(255))
    status = db.Column(db.Integer,default=1)
    name = db.Column(db.String(255))
    user_ip = db.Column(db.String(255))
    login_time = db.Column(db.Date)
    logout_time = db.Column(db.Date)
    user_agent = db.Column(db.String(255))

# 访问日志
class AccessLog(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    score = db.Column(db.Integer)
    tag = db.Column(db.String(60))
    status = db.Column(db.Boolean)
    content = db.Column(db.String(255))
    user_ip = db.Column(db.String(255))
    access_time = db.Column(db.Date)

# 设置初始化模型
def init_db(app):
    db.init_app(app)
    Migrate(app=app,db=db)